package com.framework.bas.service;

import java.util.LinkedList;
import java.util.List;

import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.UtilReflect;
import com.framework.bas.vo.BasService;

@SuppressWarnings("unchecked")
public abstract class AbsPlmService {

	private IPlmBasBS plmBasBS;
	private List prev = new LinkedList();
	private List post = new LinkedList();

	public IPlmBasBS getPlmBasBS() {
		return plmBasBS;
	}

	public void setPlmBasBS(IPlmBasBS plmBasBS) {
		this.plmBasBS = plmBasBS;
	}

	public List getPrev() {
		return prev;
	}

	public void setPrev(List prev) {
		this.prev = prev;
	}

	public List getPost() {
		return post;
	}

	public void setPost(List post) {
		this.post = post;
	}

	public Object txNewRun(GeneralParam param) {
		return handleService(param);
	}

	public Object txRun(GeneralParam param) {
		return handleService(param);
	}

	public Object runWork(GeneralParam param) {
		String serviceCode = param.getAppService();
		if (getTransactionType(serviceCode)==PlmBasConstant.TX_REQUIRED_NEW){
			return txNewRun(param);
		}else{
			return txRun(param);
		}
	}

	protected Object handleService(GeneralParam param) {
		Object result = null;
		BasService basService = param.getAppServiceObject();
		for (int i = 0; i < prev.size(); i++) {
			IService pubService = (IService) prev.get(i);
			pubService.runWork(param);
		}

		result = UtilReflect.runAllMethod(this, basService.getExecFun(),
		 new Object[]{param}, new Class[]{GeneralParam.class});
		for (int i = 0; i < post.size(); i++) {
			IService pubService = (IService) post.get(i);
			pubService.runWork(param);
		}
		return result;
	}
	
	public int getServiceType() {
		return PlmBasConstant.SERVICE_FUNC_TRADE;
	}

	public int getTransactionType(String serviceCode) {
		return PlmBasConstant.TX_REQUIRED;
	}

}
